//
//  OANameListController.m
//  OASystem
//
//  Created by zhoupengfei on 15/4/21.
//  Copyright (c) 2015年 zpf. All rights reserved.
//

#import "OANameListController.h"
#import "HttpTools.h"

#import "GDataDefines.h"
#import "MBProgressHUD.h"
#import "OANameListModel.h"
#import "OASubNameListModel.h"
#import "HeaderView.h"
#import "OANewEmailController.h"
#import "OANewSMSViewController.h"
@interface OANameListController ()<HeaderViewDelegate,HeaderViewDelegate2>




@end

@implementation OANameListController

- (void)viewDidLoad {
    [super viewDidLoad];
}

#pragma mark 取消
-(IBAction)cancle:(id)sender{
    [self.navigationController popViewControllerAnimated:YES];
}
#pragma mark 确定
-(IBAction)confir:(id)sender{
    NSMutableArray * tempArray = [NSMutableArray array];
    for (OANameListModel * nameLisModel in self.groupArray) {
        for (OASubNameListModel * subModel in nameLisModel.subArray) {
            if (subModel.isSelected) {
                [tempArray addObject:subModel];
                if ([self.delegate respondsToSelector:@selector(didSelecteName:)]) {
                    [self.delegate didSelecteName:tempArray];
                }
            }
        }
    }
    for (UIViewController * VC in self.navigationController.viewControllers) {
        if ([VC isKindOfClass:[OANewEmailController class]]||[VC isKindOfClass:[OANewSMSViewController class]]) {
//            [self.navigationController popToViewController:VC animated:YES];
  [self.navigationController popToViewController:self.navigationController.viewControllers[2] animated:YES];
        }
    }
 
}



-(void)clickHeaderView{
    [self.tableView reloadData];
}-(void)clickHeaderView2{
    [self.tableView reloadData];
}


#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
   return self.groupArray.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    OANameListModel * nameListModel = self.groupArray[section];
    NSArray * tempArray = nameListModel.subArray;
    return  nameListModel.isOpen ? tempArray.count : 0;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CellID" forIndexPath:indexPath];
    OANameListModel * nameListModel = self.groupArray[indexPath.section];
    OASubNameListModel * subModel = nameListModel.subArray[indexPath.row];
    cell.textLabel.text = subModel.value;
    cell.textLabel.font = [UIFont systemFontOfSize:13];
    if (subModel.isSelected) {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    }else{
        cell.accessoryType = UITableViewCellAccessoryNone;
    }
    return cell;
}

-(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    HeaderView * headView = [HeaderView headerViewWithTableView:tableView];
    headView.delegate = self;
    headView.groupModel= self.groupArray[section];
    return headView;
    
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    OANameListModel * nameListModel = self.groupArray[indexPath.section];
    OASubNameListModel * subModel = nameListModel.subArray[indexPath.row];
    subModel.isSelected = !subModel.isSelected;
    [self.tableView reloadData];
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 44;
}

@end
